--#include "data\functions\Activity\CombineServer\CombineServerCommon.lua" once
--合服特惠礼包


function CombineServerGiftData( sysarg )
	if not IsInCombineServerActivity() then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)

	if not sVar.combineServerGiftData then
		sVar.combineServerGiftData = {}
	end

	local giftCfg = CombineServerCfg.gift

	local netPack = DataPack.allocPacket(sysarg,LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetCombineServerGiftBagData)
	if netPack then
		local length = #giftCfg.giftData
		DataPack.writeChar(netPack, length)
		for idx, data in ipairs(giftCfg.giftData) do
			DataPack.writeChar(netPack, idx)
			if not sVar.combineServerGiftData[idx] then
				sVar.combineServerGiftData[idx] = {}
			end
			if type(sVar.combineServerGiftData[idx]) == 'number' then
				local value = sVar.combineServerGiftData[idx]
				--print("value = "..value)
				sVar.combineServerGiftData[idx] = {}
				sVar.combineServerGiftData[idx][1] = value
				--print("sVar.combineServerGiftData[idx][1] = "..sVar.combineServerGiftData[idx][1])
			end
			for ii, preData in ipairs(data) do
				local State = sVar.combineServerGiftData[idx][ii] or 0
				if State == 0 or ii == #data then
					DataPack.writeChar(netPack, ii)
					DataPack.writeChar(netPack, State)
					break
				end
			end
		end
		DataPack.flush(netPack) 
	end
end

function CombineServerGiftBagRefresh(sysarg)
	if not IsInCombineServerActivity() then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)

	if not sVar.combineServerGiftData then
		sVar.combineServerGiftData = {}
	end
	local curCombineTime = System.getServerCombineTime()
	if not sVar.combineServerGiftTime then
		sVar.combineServerGiftData = {}
		sVar.combineServerGiftTime = curCombineTime
	elseif sVar.combineServerGiftTime ~= curCombineTime then
		sVar.combineServerGiftData = {}
		sVar.combineServerGiftTime = curCombineTime
	end
end

function BuyCombineServerGift( sysarg, packet )

	if not IsInCombineServerActivity() then
		return
	end

	local giftCfg = CombineServerCfg.gift
	if not Actor.checkActorLevel(sysarg, giftCfg.nLevel, 0) then
		Actor.sendTipmsg( sysarg,string.format(Lang.ScriptTips.COM0055, giftCfg.nLevel),ttFlyTip )
		return
	end
	local idx = DataPack.readChar(packet)
	local ii = DataPack.readChar(packet)
	local ItemConfig = giftCfg.giftData[idx][ii]

	if not ItemConfig then
		return
	end

	local day = System.getDaysSinceCombineServer()
	if ItemConfig.combineDay and ItemConfig.combineDay > day then
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.CombineServerGiftBag001, ItemConfig.combineDay),ttFlyTip )
		return
	end
	
	local sVar = Actor.getStaticVar(sysarg)


	if not sVar.combineServerGiftData then
		sVar.combineServerGiftData = {}
	end

	if not sVar.combineServerGiftData[idx] then
		sVar.combineServerGiftData[idx] = {}
	end

	if ii > 1 and sVar.combineServerGiftData[idx][ii-1] ~= 1 then
		Actor.sendTipmsg( sysarg,Lang.ScriptTips.CombineServerGiftBag002,ttFlyTip )
		return
	end

	if sVar.combineServerGiftData[idx][ii] == 1 then
		return
	end

	if CheckBagGridForAwardsEx(sysarg,ItemConfig.awards) > 0 then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0015, ttFlyTip )
		return
	end

	if Actor.getMoneyCount(sysarg, mtYuanbao) < ItemConfig.yuanbao then
		Actor.sendItemLack( sysarg, qatYuanbao, 0, ItemConfig.yuanbao )
		--Actor.sendTipmsg( sysarg,string.format(Lang.ScriptTips.COM0053, ItemConfig.yuanbao),ttFlyTip )
	end

	if not Actor.changeMoney(sysarg,mtYuanbao, -ItemConfig.yuanbao, LogIds.CombineServerGiftBagCostLogId, Lang.LogTips.CombineServerGiftBag001) then
		return
	end

	sVar.combineServerGiftData[idx][ii] = 1

	GiveCommonAwardEx(sysarg, ItemConfig.awards, LogIds.CombineServerGiftBagAwardLogId, Lang.LogTips.CombineServerGiftBag002)

	CombineServerGiftData(sysarg)

	local actorName = Actor.getName(sysarg)
	local msg = string.format(Lang.ScriptTips.CombineServerGiftBag003, actorName, ItemConfig.sItemName, enLinkCombineServerGiftBag)		
	System.broadcastTipmsg(msg, ttChatWindow)

end
